package com.example.demo.mapper;


import com.example.demo.auxiliary.UserSqlProvider;
import com.example.demo.identity.RecommendFriends;
import com.example.demo.identity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Mapper
public interface UserMapper {

    //    这个应用于用户登录信息查询的，后期更改
    @Select("select * from common where username = #{username}")
    User checkUser(@RequestBody User user);

    //    通过uuId获取用户信息
    @Select("select * from common where uu_id = #{uuId}")
    User getUserByUUID(@Param("uuId") String uuId);

    //    查询用户信息
    @Select("select username, gex, province, city, uu_id," +
            "avatar,SignatureOfTheIndividual, country, birthday from common where username = #{username}")
    User getUserInfo(@RequestBody User user);

    //    通过uu_id获取用户的key、label、avatar信息
    @Select("select uu_id, username, avatar from common where uu_id = #{uuId}")
    User getFriendUserByUuId(@Param("uuId") String uuId);

    //    更新用户信息
    @Update("update common set uu_id = #{uu_id} where username = #{username}")
    void updateUserUuId(@Param("uu_id") String uu_id, @Param("username") String username);

    @Update("update common set token = #{token} where username = #{username}")
    void updateToken(@Param("token") String token, @Param("username") String username);

    @Insert("insert into common(username, password, uu_id, avatar, token) values (#{username}, #{password}, #{uu_id}, #{avatar},#{token})")
    int insertUser(@RequestBody User user);

    //    随机获取用户信息
    @Select("select username, uu_id, gex, province, city," +
            "TypeOfFriendShips, avatar,SignatureOfTheIndividual, country from common order by RAND() limit 20")
    List<RecommendFriends> recommendFriends();

    //    动态更新用户（只更新非空字段）       这里如果不写Update就不能完成对应的sql操作，不知道为什么也不发映射xml中的sql
    @UpdateProvider(type = UserSqlProvider.class, method = "updateUserBasicInfo")
    int dynamicUpdates(@Param("uuId") String uuId, User user);

//    更新用户上传的头像信息
    @Update("update common set avatar = #{avatar} where uu_id = #{userId}")
    void updateAvatar(@Param("userId") String userId, @Param("avatar") String avatar);


    //    更新用户交友标签          这个暂时不用
    @Update("update common set TypeOfFriendShips = #{TypeOfFriendShip} where uu_id = #{userId}")
    void updateTypeOfFriendShips(@Param("userId") String userId, @Param("TypeOfFriendShip") String typeOfFriendShip);

    //    通过用户id获取用户avatar
    @Select("select avatar from common where uu_id = #{uuId}")
    String getAvatar(@Param("uuId") String uuId);

    //    更新用户个人信息
    @UpdateProvider(type = UserSqlProvider.class, method = "updateUserBasicInfo")
    int updateUserBasicInfo(@Param("uuId") String uuId, @Param("user") User user);

    //    获取所有用户的uuId
    @Select("select uu_id from common")
    List<String> getAllUuId();


}
